
/* Generated by Interface Builder */

#import "MyObject.h"
#import <appkit/Application.h>
#import <appkit/Matrix.h>
#import <appkit/TextField.h>
#import <libc.h>
#import <math.h>
#import "MesaAPI.h"
#import <appkit/OpenPanel.h>
#import "MyListen.h"
#import <appkit/NXImage.h>

#define MY_DELAY 2000
#define rows 3

@implementation MyObject

- doWalk:sender
{
	[self perform:@selector(startWalk:) with:self afterDelay:MY_DELAY cancelPrevious:NO];
	
    return self;
}

- startWalk:sender
{
	int x,num;
	id t;
	double d,d1;
	char *stocks[rows];
	const char *p;
	MAPIValue values[rows],*mv,*mv2;
	int ur,uc,lr,lc,r,c;
	id myImage;
	NXPoint np;
	
	if (!sender) return self;
	
	for (x = 0,num = 0; x < rows; x++) {
		p = [[stockMatrix cellAt:x :0] stringValue];
		if (strlen((char *) p) && (d = [(t = [stockMatrix cellAt:x :1]) doubleValue])) {
			d1 = d * 0.1;
			d1 *= (double) ((double) random() / (double) 2147483647);
			d1 -= d * 0.05;
			d += d1;
			if (d < 10) d = 10;
			[t setDoubleValue:rint(d)];
			stocks[num] = (char *) p;
			values[num].type = numberMAPIValue;
			values[num].values.number = rint(d);
			num++;
			}
		}
	
	if (num) {
		[mapi associateLabel:"prices" withItems:stocks andValues:values offset:1 numItems:num
			orientation:verticalOrientation];
			
		[mapi recalc];
		
		if ([mapi getValues:&mv forLabel:"summary" num:&num upperRow:&ur col:&uc
			lowerRow:&lr col:&lc] == MAPISuccess) {
			
			for (x = 0; x < rows; x++) {
				[[portMatrix cellAt:x :0] setStringValue:""];
				[[portMatrix cellAt:x :1] setStringValue:""];
				}
			
			for (x = 0,mv2 = mv; x < num; x++,mv2++) {
				r = mv2 -> row - ur;
				c = mv2 -> col - uc;
				if (r < rows && c < 2) {
					t = [portMatrix cellAt:r :c];
					if (mv2 -> type == stringMAPIValue) [t setStringValue:mv2 -> values.string];
					else if (mv2 -> type == numberMAPIValue)
						[t setDoubleValue:mv2 -> values.number];
					}
				}
			
			freeMAPIValue(num,mv);
			}
			
		[mapi getEPSForLabel:"summary" in:&myImage];
		if (myImage) {
			[theView lockFocus];
			np.x = np.y = 0;
			[myImage composite:NX_COPY toPoint:&np];
			[theView unlockFocus];
			[myImage free];
			}
			
		[mapi getEPSForGraph:"Graph 1" in:&myImage];
		if (myImage) {
			[theGraph lockFocus];
			np.x = np.y = 0;
			[myImage composite:NX_COPY toPoint:&np];
			[theGraph unlockFocus];
			[myImage free];
			}
			
		
		}
		
	[self perform:@selector(startWalk:) with:self afterDelay:MY_DELAY cancelPrevious:NO];
	
    return self;
	
}

- endWalk:sender
{
 	[self perform:@selector(startWalk:) with:NULL afterDelay:0 cancelPrevious:YES];
   return self;
}

- appDidInit:sender
{
	id op;
	static char *types[] = {"Mesa",NULL};
	char tmp[2000];
	char **fn;
	
	op = [OpenPanel new];
	[op allowMultipleFiles:NO];
	if ([op runModalForTypes:types]) {
		if ((fn = (char **) [op filenames])) {
			sprintf(tmp,"%s/%s",[op directory],fn[0]);
			mapi = [[MesaAPI alloc] initToWorksheet:tmp];
			}
		}
	else [NXApp terminate:self];
	
	listen = [[MyListen alloc] initToPort:"LowValue"];
	[listen setMatrix:listenMatrix];
	
	return self;
	}
	
- appWillTerminate:sender
{
	[NXApp setDelegate:NULL];
	[listen free];
	[mapi free];
	[self free];
	
	return sender;
	}

- printTheReport:sender
{
	[mapi printReport:"Portfolio"];
	
	return self;
	}
@end
